<template>
    <el-container direction="vertical" style="overflow: hidden">
      <el-main style="padding: 20px; height: calc(100vh - 124px); overflow: auto">
        <div class="search-bar">
          <span class="page-title">班级管理-详情</span>
          <el-input v-model="input" style="width: 240px" placeholder="Please input" />
          <div class="date-search">
            <el-input
              v-model="input1"
              style="width: 240px"
              placeholder="按时间段搜索"
              :suffix-icon="Calendar"
            />
          </div>
          <el-button type="primary" icon="Search">搜索</el-button>
          <el-button @click="handleAdd">添加班级</el-button>
        </div>
        <el-table :data="tableData" style="width: 100%; margin-top: 20px" stripe border>
          <el-table-column prop="name" label="班级序号" align="center" />
          <el-table-column prop="gender" label="班级名称" align="center" />
          <el-table-column prop="phone" label="班级人数" align="center" />
          <el-table-column prop="address" label="开始时间" align="center" />
          <el-table-column prop="emergencyContact" label="结束时间" align="center" />
          <el-table-column prop="emergencyPhone" label="班级老师" align="center" />
          <el-table-column prop="emergencyPhone" label="班级状态" align="center" />
          <el-table-column label="操作" align="center">
            <template #default="{ row }">
              <el-button size="mini" type="text" @click="() => handleEdit(row)">修改</el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleBusinessClassDetail"
                style="color: red"
              >详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        />
        <el-dialog
          v-model="dialogVisible"
          title="修改班级"
          width="400px"
          :show-close="false"
        >
          <el-form
            :model="formData"
            :rules="rules"
            ref="formRef"
            label-width="100px"
          >
            <el-form-item
              label="班级名称"
              prop="className"
            >
              <el-input v-model="formData.className" />
            </el-form-item>
            <el-form-item
              label="开始时间"
              prop="startTime"
            >
              <el-date-picker
                v-model="formData.startTime"
                type="date"
                placeholder="请选择开始时间"
              />
            </el-form-item>
            <el-form-item
              label="结束时间"
              prop="endTime"
            >
              <el-date-picker
                v-model="formData.endTime"
                type="date"
                placeholder="请选择结束时间"
              />
            </el-form-item>
            <el-form-item
              label="班级负责人"
              prop="classLeader"
            >
              <el-input v-model="formData.classLeader" />
            </el-form-item>
            <el-form-item
              label="学科"
              prop="subject"
            >
              <el-select
                v-model="formData.subject"
                placeholder="请选择学科"
              >
                <el-option
                  v-for="option in subjectOptions"
                  :key="option.value"
                  :label="option.label"
                  :value="option.value"
                />
              </el-select>
            </el-form-item>
          </el-form>
          <template #footer>
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="handleConfirm">确认</el-button>
          </template>
        </el-dialog>
      </el-main>
    </el-container>
  </template>
  
  <script lang="ts" setup>
  import { onMounted, onUnmounted, ref } from 'vue'
import { useRouter } from 'vue-router' // 引入 useRouter

// 初始化路由对象
const router = useRouter()
  import { Calendar, Search } from '@element-plus/icons-vue'
  import { ElTable, ElTableColumn, ElPagination, ElDialog, ElForm, ElFormItem, ElInput, ElDatePicker, ElSelect, ElOption } from 'element-plus'
  import { Location } from '@element-plus/icons-vue'

  const input = ref('')
  const input1 = ref('')
  // 表格数据
  const tableData = ref([
    {
      name: '1',
      gender: '前端',
      phone: '2021.4.12',
      address: '2024.1.12',
      emergencyContact: '白玉川',
      emergencyPhone: '已开班'
    }
  ])
  const currentPage = ref(1)
  const pageSize = ref(10)
  const total = ref(0)
  
  // 编辑方法
  const dialogVisible = ref(false);
  const subjectOptions = ref([
    { label: '前端', value: '0' },
    { label: '鸿蒙', value: '1' },
    { label: 'Java', value: '2' }
  ]);
  const formData = ref({
    className: '',
    startTime: '',
    endTime: '',
    classLeader: '',
    subject: ''
  });
  const rules = ref({
    className: [
      { required: true, message: '请输入班级名称', trigger: 'blur' }
    ],
    startTime: [
      { required: true, message: '请选择开始时间', trigger: 'change' }
    ],
    endTime: [
      { required: true, message: '请选择结束时间', trigger: 'change' }
    ],
    classLeader: [
      { required: true, message: '请输入班级负责人', trigger: 'blur' }
    ],
    subject: [
      { required: true, message: '请选择学科', trigger: 'change' }
    ]
  });
  // 标识是添加还是修改操作，true为添加，false为修改
  const isAdd = ref(false); 
  
  // 点击添加班级按钮方法
  const handleAdd = () => {
    isAdd.value = true;
    // 重置表单数据
    formData.value = {
      className: '',
      startTime: '',
      endTime: '',
      classLeader: '',
      subject: ''
    };
    dialogVisible.value = true;
  };
  
  // 点击修改按钮方法优化
  const handleEdit = (row) => {
    isAdd.value = false;
    formData.value.className = row.gender; 
    formData.value.startTime = row.address; 
    formData.value.endTime = row.emergencyContact; 
    formData.value.classLeader = row.emergencyPhone; 
    formData.value.subject = row.name; 
    dialogVisible.value = true;
  };
  

const handleBusinessClassDetail = () => {
  
};
  
  // 页码切换处理方法
  const handleCurrentChange = (page: number) => {
    currentPage.value = page
    // 这里后续可以添加调用接口获取对应页码数据的逻辑
  }
  
  // 获取当前时间
  const currentTime = ref('')
  
  const updateTime = () => {
    const now = new Date()
    const year = now.getFullYear()
    const month = String(now.getMonth() + 1).padStart(2, '0')
    const day = String(now.getDate()).padStart(2, '0')
    const hour = String(now.getHours()).padStart(2, '0')
    const minute = String(now.getMinutes()).padStart(2, '0')
    const second = String(now.getSeconds()).padStart(2, '0')
    currentTime.value = `${year}-${month}-${day} ${hour}:${minute}:${second}`
  }
  
  onUnmounted(() => {
    // 清理定时器
    // clearInterval(updateTime)
    // 清理相关逻辑
  })
  
  const formRef = ref(null);
  const handleConfirm = () => {
    formRef.value.validate((valid) => {

    });
  };
  </script>
  
  <style scoped>
  #one {
    height: 100vh;
    overflow: hidden;
  }
  
  .header-logo {
    display: flex;
    align-items: center;
  }
  
  .header-one {
    display: flex;
    width: 30%;
    justify-content: space-around;
    align-items: center;
  }
  
  .header-time {
    font-size: 16px;
    color: #333;
  }
  
  .header-icon {
    width: 20px;
    height: 20px;
    cursor: pointer;
    transition: transform 0.2s ease;
  }
  
  .header-icon:hover {
    transform: scale(1.1);
  }
  
  .header-user {
    display: flex;
    align-items: center;
    gap: 5px;
  }
  .el-container {
    width: 100%;
    height: 100%;
  }
  
  .el-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }
  
  .el-main {
    background: #f0f2f5;
  }
  
  .search-bar {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    background-color: #fff;
    padding: 20px;
  }
  
  .page-title {
    font-size: 20px;
    font-weight: bold;
    margin-right: 20px;
  }
  
  .date-search {
    display: flex;
    align-items: center;
    gap: 10px;
  }
  
  .el-table__body-wrapper {
    overflow: auto;
  }
  
  .el-table__body tr {
    transition: background-color 0.2s ease;
  }
  
  .el-table__body tr:hover {
    background-color: #ecf5ff;
  }
  
  .el-table__body td {
    padding: 8px 12px;
  }
  
  .el-table__header th {
    background-color: #f5f7fa !important;
    color: #606266;
    padding: 10px 12px;
  }
  
  .el-pagination {
    margin-top: 10px;
    text-align: center;
  }
  
  .el-button--text {
    margin-right: 5px;
  }
  
  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #666;
    font-size: 14px;
  }
 :deep(.el-dialog header span){
   display: inline-block;
   height: 50px;
   background-color: red;
   width: 100%;
   color: skyblue;
  }
  :deep(.el-dialog) {
    padding: 0;
   background-color: red;
  }
  </style>